define([ './module' ], function(goodsmodule) {
	'use strict';

	goodsmodule.controller('goods',goodsCtrl);
	function goodsCtrl($stateParams,$http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer,Upload,$timeout,GoodsTypeService,goodscategoryService){
		$scope.title="货物列表";
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="gt.selectAll" ng-click="gt.toggleAll(gt.selectAll,gt.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
				type:'POST',
				url:'../mall/products/findProductByPage',
				dataSrc:'data'
			})
			.withOption('processing',true)
			.withOption('paging',true)
			.withOption('serverSide',true)
			.withOption('createdRow',function(row,data,dataIndex){
				$compile(angular.element(row).contents())($scope);
			})
			.withOption('headerCallback', function(header) {
				if (!$scope.headerCompiled) {
					$scope.headerCompiled = true;
					$compile(angular.element(header).contents())($scope);
				}
			})
			.withOption('stateSave', true)
			.withOption('rowCallback',rowCallback)
			.withPaginationType('full_numbers')
			.withLanguageSource('./app/language/chinese.json')

		$scope.$on('handleRequest',function(){

		});
		vm.dtColumns=[
			DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
				vm.selected[full.id]=false;
				return "<input type='checkbox' ng-model=\"gt.selected['"+data.id+"']\" ng-click=\"gt.toggleOne(gt.selected)\">";
			}),
			DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			DTColumnBuilder.newColumn(null).withTitle('图片').notSortable().renderWith(imgHtml),
			DTColumnBuilder.newColumn('psn').withTitle('货号').notSortable(),
			DTColumnBuilder.newColumn('productname').withTitle('货物名称').notSortable(),
			DTColumnBuilder.newColumn('nname').withTitle('分类').notSortable(),
			DTColumnBuilder.newColumn('goodstypename').withTitle('商品类型').notSortable(),
			DTColumnBuilder.newColumn('stock').withTitle('库存').notSortable(),
			DTColumnBuilder.newColumn('saleprice').withTitle('销售价').notSortable(),
			DTColumnBuilder.newColumn('memberprice').withTitle('会员价').notSortable(),
			DTColumnBuilder.newColumn('salestatus').withTitle('销售状态').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable()];
		function imgHtml(data,type,full,meta){
			return '<img style="width:50px;height:50px;" ng-src="'+data.mainimg+'" alt="Photo" />';
		}

		function reloadData(){
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}


		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:'',
				psn:'',
				goodstypeid:''
			};
			if($scope.productname!=undefined){
				searchParams.name=$scope.productname;

			}
			if($scope.psn!=undefined){
				searchParams.psn=$scope.psn;
			}
			if($scope.selectedgoodstype.goodstypeid!=undefined){
				searchParams.goodstypeid=$scope.selectedgoodstype.goodstypeid;
			}else{
				$scope.errors = {
					show: true,
					msg: '请选择商品类型'
				}
				return false;
			}
			vm.dtOptions['ajax'].data=searchParams;
			vm.dtInstance.reloadData(callback, resetPaging);
		}
		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;

		}

		function someClickHandler(info) {
			vm.message = info.id + ' - ' + info.name;
			console.log("======"+info);
			$scope.pushToSelectedProducts(info.id,info);
		}
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
			$('td', nRow).bind('click', function() {
				$scope.$apply(function() {
					vm.someClickHandler(aData);
				});
			});
			return nRow;
		}



		//查询所有商品类型
		$scope.findallgoodstype = function () {
			var promise = GoodsTypeService.query();
			promise.then(function (data) {
				$scope.goodstypeselect = data.beanlist;
			}, function (data) {
				$scope.errors = {
					show: true,
					msg: '系统异常'
				}
			});
		};

		$scope.goodstypechange = function () {
			if ($scope.selectedgoodstype != undefined) {
				$http({
					method: 'POST',
					url: '../mall/goodstypebrand/findAllTypeBrands',
					data: {
						'goodstypeid': $scope.selectedgoodstype.goodstypeid
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.errors = {};
						$scope.brandsselect = [];
						$scope.brandsselect = angular.fromJson(data.beanlist[0].brandlist);
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});

			}
			;
			//填充商品属性
			$http({
				method: 'POST',
				url: '../mall/goodsattribute/findAllTypeGoodsAttrs',
				data: {
					'goodstypeid': $scope.selectedgoodstype.goodstypeid
				}
			}).success(function (data, status, headers, config) {
				if (data.sucflag) {
					$scope.errors = {};
					$scope.goodsattrs = [];
					angular.forEach(data.beanlist, function (d, i, a) {
						$scope.goodsattrs.push(d);
					});
				}
			}).error(function (data, status, headers, config) {
				$scope.errors = {
					show: true,
					msg: '系统异常'
				}
			});
		};

		//获取所有一级商品分类
		$scope.findNavAll = function () {
			var promise = goodscategoryService.query();
			promise.then(function (data) {
				$scope.navselect = data.beanlist;
			}, function (data) {
				$scope.errors = {
					show: true,
					msg: '系统异常'
				}
			});
		};
		//获取所有二级商品分类
		$scope.navchange = function () {
			if ($scope.selectednav != undefined) {
				$http({
					method: 'POST',
					url: '../mall/goodscategory/findCategoryAllByParentId',
					data: {
						'id': $scope.selectednav.id
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag && data.beanlist.length > 0) {
						$scope.errors = {};
						$scope.ltypeselect = [];
						$scope.ltypeselect = angular.fromJson(data.beanlist);
						$scope.ltypecategoryshow = true;
					} else {
						$scope.ltypecategoryshow = false;
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});

			};
		};
		//获取所有三级商品分类
		$scope.ltypechange = function () {
			if ($scope.selectedltype != undefined) {
				$http({
					method: 'POST',
					url: '../mall/goodscategory/findCategoryAllByParentId',
					data: {
						'id': $scope.selectedltype.id
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag && data.beanlist.length > 0) {
						$scope.errors = {};
						$scope.stypeselect = [];
						$scope.stypeselect = angular.fromJson(data.beanlist);
						$scope.stypeidcategoryshow = true;
					} else {
						$scope.stypeidcategoryshow = false;
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});

			}
			;
		};

		$scope.$watch('rmainfiles', function () {
			$scope.uploadrmainfiles($scope.rmainfiles);
		});
		$scope.allrmainfiles = [];
		$scope.uploadrmainfiles = function (files) {
			if (files && files.length) {
				for (var i = 0; i < files.length; i++) {
					var file = files[i];
					var filename = files[i].name;
					if (!file.$error) {
						Upload.upload({
							url: '../mall/files/ajaxFileUploadsImgs',
							data: {
								file: file,
								filename: filename
							}
						}).progress(function (evt) {
							var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
							$scope.rmainprogress = progressPercentage;
						}).success(function (data, status, headers, config) {
							$timeout(function () {
								if (data.ifb.sucflag) {
									$scope.allrmainfiles.push(data.ifb);
								}
							});
						});
					}
				}
			}
		}

		$scope.removeallrmainfiles = function (index) {
			$scope.allrmainfiles.splice(index, 1);
		}

		$scope.$watch('rfiles', function () {
			$scope.uploadrfiles($scope.rfiles);
		});
		$scope.allrfiles = [];
		$scope.uploadrfiles = function (files) {
			if (files && files.length) {
				for (var i = 0; i < files.length; i++) {
					var file = files[i];
					var filename = files[i].name;
					if (!file.$error) {
						Upload.upload({
							url: '../mall/files/ajaxFileUploadsImgs',
							data: {
								file: file,
								filename: filename
							}
						}).progress(function (evt) {
							var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
							$scope.rprogress = progressPercentage;
						}).success(function (data, status, headers, config) {
							$timeout(function () {
								if (data.ifb.sucflag) {
									$scope.allrfiles.push(data.ifb);
								}
							});
						});
					}
				}
			}
		}

		$scope.removeallrfiles = function (index) {
			$scope.allrfiles.splice(index, 1);
		}


		//选定货物
		var sp = [];
		var spindex=[];
		$scope.pushToSelectedProducts = function (index, product) {
			sp.push(product);
			$scope.selectedproducts = sp;
			$scope.minprice = product.memberprice;
			$scope.maxprice = product.memberprice;
			//修改最高价格和最低价格
			angular.forEach($scope.selectedproducts, function (d, i, a) {
				if (d.memberprice < $scope.minprice) {
					$scope.minprice = d.memberprice;
				}
				if (d.memberprice > $scope.maxprice) {
					$scope.maxprice = d.memberprice;
				}
			});
			//修改总的虚拟销量
			$scope.allvsales = product.vsales;
			//修改总真实销量
			$scope.allsales = product.sales;
			//修改总评价
			$scope.alltotalcomment = product.totalcomment;
			//修改总点赞
			$scope.alltotalzan = product.totalzan;
		};

		/**
		 * 删除选择的货物
		 * @param index
         */
		$scope.removeselectproduct=function(index){
			$scope.selectedproducts.splice(index,1);
		}
		//修改商品名称时自动填充搜索关键字和描述
		$scope.$watch('goodsname', function () {
			$scope.metakey = $scope.metades = $scope.goodsname;
		});

		//通过location中的operate参数区分操作行为
		var operate = $location.search().operate||$stateParams.operate;
		if (operate != undefined && operate == "save") {
			//控制保存按钮显示
			$scope.savebtn = {show: true};
			//默认servicecode
			$scope.servicecode = "code";
			$scope.title = '添加商品';
			//隐藏上一个分类
			$scope.lastcategoryshow = false;
			$scope.modifycategoryshow = false;
			//显示一级分类选择
			$scope.navcategoryshow = true;
			//隐藏二级分类选择
			$scope.ltypecategoryshow = false;
			//隐藏三级分类选择
			$scope.stypeidcategoryshow = false;
			//默认选择站内商品
			$scope.isoutsite = "1";
			//显示站内商品区域
			$scope.outsiteshow = true;
			//隐藏外链
			$scope.outsitelinkshow = false;
			//显示商品类型
			$scope.goodstypeshow = true;
			//隐藏上一个商品类型
			$scope.lastgoodstypenameshow = false;
			$scope.modifyfoodtypeshow = false;
			//隐上一个品牌
			$scope.lastbrandnameshow = false;
			$scope.modifybrandnameshow = false;
			$scope.brandnameshow = true;
			//设置同步移动平台1表示同步
			$scope.msync = "1";
			//设置启用 1表示启用
			$scope.status = "1";
			//默认排序
			$scope.sort = 1;

			$scope.isoutsitechange = function () {
				if ($scope.isoutsite == "1") {
					$scope.outsiteshow = true;
					$scope.outsitelinkshow = false;
				} else {
					$scope.outsiteshow = false;
					$scope.outsitelinkshow = true;
				}
			};

			$scope.save = function () {
				var outsitelink = "";
				if ($scope.isoutsite == "0") {
					outsitelink = $scope.outsitelink;
				}
				var productids = "";
				if ($scope.selectedproducts == undefined && $scope.isoutsite == "1") {
					$scope.errors = {
						show: true,
						msg: '请选择货物'
					}
					return false;
				} else {
					var temparray = [];
					angular.forEach($scope.selectedproducts, function (d, i, a) {
						temparray.push(d.id);
					});
					productids = temparray.join(",");
				}

				if ($scope.selectedgoodstype == undefined) {
					$scope.errors = {
						show: true,
						msg: '请选择商品类型'
					}
					return false;
				}
				if ($scope.selectedbrands == undefined) {
					$scope.errors = {
						show: true,
						msg: '请选择商品品牌'
					}
					return false;
				}
				if ($scope.selectednav == undefined) {
					$scope.errors = {
						show: true,
						msg: '请选择一级分类'
					}
					return false;
				}

				var ltypeid = "";
				var lname = "";
				if ($scope.selectedltype != undefined) {
					ltypeid = $scope.selectedltype.id;
					lname = $scope.selectedltype.name;
				}

				var stypeid = "";
				var sname = "";
				if ($scope.selectedstype != undefined) {
					stypeid = $scope.selectedltype.id;
					sname = $scope.selectedltype.name;
				}

				if ($scope.goodsname == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品标题'
					}
					return false;
				}
				if ($scope.subgoodsname == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品副标题'
					}
					return false;
				}
				if ($scope.sort == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品排序'
					}
					return false;
				}
				if ($scope.commoditylist == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品清单'
					}
					return false;
				}
				if ($scope.detail == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品介绍'
					}
					return false;
				}
				if ($scope.allrmainfiles.length == 0) {
					$scope.errors = {
						show: true,
						msg: '请上传主图'
					}
					return false;
				}
				//if($scope.allrfiles.length==0){
				//	$scope.errors={
				//			show:true,
				//			msg:'请上传主图'
				//	}
				//	return false;
				//}
				$http({
					method: 'POST',
					url: '../mall/goods/save',
					data: {
						"allvsales": $scope.allvsales,
						"allsales": $scope.allsales,
						"alltotalcomment": $scope.alltotalcomment,
						"alltotalzan": $scope.alltotalzan,
						"servicecode": $scope.servicecode,
						"isoutsite": $scope.isoutsite,
						"outsitelink": outsitelink,
						"productids": productids,
						"goodstypeid": $scope.selectedgoodstype.goodstypeid,
						"goodstypename": $scope.selectedgoodstype.name,
						"brandid": $scope.selectedbrands.brandid,
						"brandname": $scope.selectedbrands.name,
						"navid": $scope.selectednav.id,
						"nname": $scope.selectednav.name,
						"ltypeid": ltypeid,
						"lname": lname,
						"stypeid": stypeid,
						"sname": sname,
						"goodsname": $scope.goodsname,
						"subgoodsname": $scope.subgoodsname,
						"minprice": $scope.minprice,
						"maxprice": $scope.maxprice,
						"metakey": $scope.metakey,
						"metades": $scope.metades,
						"commoditylist": $scope.commoditylist,
						"mainimg": angular.toJson($scope.allrmainfiles),
						"imgs": angular.toJson($scope.allrfiles),
						"detail": $scope.detail,
						"msync": $scope.msync,
						"sort": $scope.sort,
						"status": $scope.status
					}
				})
					.success(function (data, status, headers, config) {
						if (data.sucflag) {
							$scope.errors = {};
							$scope.info = {
								show: true,
								msg: '商品添加成功'
							}
							$location.path('/goodsment');
						}
					}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});

			}
		}

		if (operate == 'edit') {
			var id = $location.search().id;
			if (id != undefined && id != "") {
				$http({
					method: 'POST',
					url: '../mall/goods/find',
					data: {
						'goodsid': id
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.title = '正在对商品--' + data.bean.goodsname + '--进行编辑';
						$scope.isoutsite = data.bean.isoutsite;
						if ($scope.isoutsite == "1") {
							//站内商品
							$scope.outsiteshow = true;
							$scope.outsitelinkshow = false;
							//填充选择的货物
							$scope.selectedproducts = data.pbeanlists;
						} else {
							//站外商品
							$scope.outsiteshow = false;
							$scope.outsitelinkshow = true;
							$scope.outsitelink = data.bean.outsitelink;
						}
						//显示选择的商品类型
						$scope.lastgoodstypenameshow = true;
						$scope.lastgoodstypename = data.bean.goodstypename;
						$scope.goodstypeid = data.bean.goodstypeid;
						$scope.modifygoodstypeshow = true;
						$scope.goodstypeshow = false;
						//显示选择的商品品牌
						$scope.lastbrandnameshow = true;
						$scope.brandid = data.bean.brandid;
						$scope.lastbrandname = data.bean.brandname;
						$scope.modifybrandnameshow = true;
						$scope.brandnameshow = false;
						//$scope.goodsattrs = angular.fromJson(data.bean.pattrs);
						//显示分类
						$scope.lastcategoryshow = true;
						$scope.lastcategory = data.bean.nname + "-" + data.bean.lname + "-" + data.bean.sname;
						$scope.navid = data.bean.navid;
						$scope.nname = data.bean.nname;
						$scope.ltypeid = data.bean.ltypeid;
						$scope.lname = data.bean.lname;
						$scope.stypeid = data.bean.stypeid;
						$scope.sname = data.bean.sname;
						//显示分类修改
						$scope.modifycategoryshow = true;
						$scope.goodsname = data.bean.goodsname;
						$scope.subgoodsname = data.bean.subgoodsname;
						$scope.minprice = data.bean.minprice;
						$scope.maxprice = data.bean.maxprice;
						$scope.allvsales = data.bean.allvsales;
						$scope.allsales = data.bean.allsales;
						$scope.alltotalcomment = data.bean.alltotalcomment;
						$scope.alltotalzan = data.bean.alltotalzan;
						$scope.metakey = data.bean.metakey;
						$scope.metades = data.bean.metades;
						$scope.msync = data.bean.msync;
						$scope.sort = data.bean.sort;
						$scope.status = data.bean.status;
						$scope.commoditylist = data.bean.commoditylist;
						$scope.detail = data.gdbean.detail;
						$scope.allrmainfiles.push(data.ifb);
						$scope.allrfiles = data.ifbs;
						$scope.servicecode = data.bean.servicecode;
						$scope.updatebtn = {show: true};
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}

			//点击修改商品类型按钮
			$scope.clickmodifygoodstype = function () {
				$scope.goodstypeshow = true;
			}
			//点击修改品牌
			$scope.clickmodifybrandname = function () {
				$scope.brandnameshow = true;
			}
			//点击修改分类
			$scope.clickmodifycategory = function () {
				$scope.navcategoryshow = true;
			}

			$scope.update = function () {
				var outsitelink = "";
				if ($scope.isoutsite == "0") {
					outsitelink = $scope.outsitelink;
				}
				var productids = "";
				if ($scope.selectedproducts == undefined && $scope.isoutsite == "1") {
					$scope.errors = {
						show: true,
						msg: '请选择货物'
					}
					return false;
				} else {
					var temparray = [];
					angular.forEach($scope.selectedproducts, function (d, i, a) {
						temparray.push(d.productid);
					});
					productids = temparray.join(",");
				}

				var goodstypeid = "";
				var goodstypename = "";
				if ($scope.goodstypeshow) {
					if ($scope.selectedgoodstype == undefined) {
						$scope.errors = {
							show: true,
							msg: '请选择商品类型'
						}
						return false;
					} else {
						goodstypeid = $scope.selectedgoodstype.goodstypeid;
						goodstypename = $scope.selectedgoodstype.name;
					}
				} else {
					goodstypeid = $scope.goodstypeid;
					goodstypename = $scope.lastgoodstypename;
				}
				var brandid = "";
				var brandname = "";
				if ($scope.brandnameshow) {
					if ($scope.selectedbrands == undefined) {
						$scope.errors = {
							show: true,
							msg: '请选择品牌'
						}
						return false;
					} else {
						brandid = $scope.selectedbrands.brandid;
						brandname = $scope.selectedbrands.brandname;
					}
				} else {
					brandid = $scope.brandid;
					brandname = $scope.lastbrandname;
				}
				if ($scope.navcategoryshow) {
					var navid = "";
					var nname ="";
					var ltypeid = "";
					var lname = "";
					var stypeid = "";
					var sname = "";
					if ($scope.selectednav == undefined) {
						$scope.errors = {
							show: true,
							msg: '请选择一级分类'
						}
						return false;
					} else {
						navid = $scope.navid;
						nname = $scope.nname;
					}
					if ($scope.selectedltype != undefined) {
						ltypeid = $scope.selectedltype.id;
						lname = $scope.selectedltype.name;
					} else {
						ltypeid = $scope.ltypeid;
						lname = $scope.lname;
					}

					if ($scope.selectedstype != undefined) {
						stypeid = $scope.selectedltype.id;
						sname = $scope.selectedltype.name;
					} else {
						stypeid = $scope.stypeid;
						sname = $scope.sname;
					}
				} else {
					navid = $scope.navid;
					nname = $scope.nname;
					ltypeid = $scope.ltypeid;
					lname = $scope.lname;
					stypeid = $scope.stypeid;
					sname = $scope.sname;
				}

				if ($scope.goodsname == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品标题'
					}
					return false;
				}
				if ($scope.subgoodsname == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品副标题'
					}
					return false;
				}
				if ($scope.sort == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品排序'
					}
					return false;
				}
				if ($scope.commoditylist == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品清单'
					}
					return false;
				}
				if ($scope.detail == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入商品介绍'
					}
					return false;
				}
				if ($scope.allrmainfiles.length == 0) {
					$scope.errors = {
						show: true,
						msg: '请上传主图'
					}
					return false;
				}
				//if($scope.allrfiles.length==0){
				//	$scope.errors={
				//			show:true,
				//			msg:'请上传主图'
				//	}
				//	return false;
				//}

				$http({
					method: 'POST',
					url: '../mall/goods/update',
					data: {
						"allvsales": $scope.allvsales,
						"allsales": $scope.allsales,
						"alltotalcomment": $scope.alltotalcomment,
						"alltotalzan": $scope.alltotalzan,
						"servicecode": $scope.servicecode,
						"isoutsite": $scope.isoutsite,
						"outsitelink": outsitelink,
						"productids": productids,
						"goodstypeid": goodstypeid,
						"goodstypename": goodstypename,
						"brandid": brandid,
						"brandname": brandname,
						"navid": navid,
						"nname": nname,
						"ltypeid": ltypeid,
						"lname": lname,
						"stypeid": stypeid,
						"sname": sname,
						"goodsname": $scope.goodsname,
						"subgoodsname": $scope.subgoodsname,
						"minprice": $scope.minprice,
						"maxprice": $scope.maxprice,
						"metakey": $scope.metakey,
						"metades": $scope.metades,
						"commoditylist": $scope.commoditylist,
						"mainimg": angular.toJson($scope.allrmainfiles),
						"imgs": angular.toJson($scope.allrfiles),
						"detail": $scope.detail,
						"msync": $scope.msync,
						"sort": $scope.sort,
						"status": $scope.status,
						"goodsid": id
					}
				})
					.success(function (data, status, headers, config) {
						if (data.sucflag) {
							$scope.errors = {};
							$scope.info = {
								show: true,
								msg: '商品更新成功'
							}
							$location.path('/goodsment');
						}
					}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});

			}
			//点击修改菜系
			$scope.clickmodifyfoodtype = function () {
				$scope.foodtypeshow = true;
			}
		}




	}


	goodsmodule.controller('goodsment',ServerGoodsmentCtrl);
	//查询列表数据
	function ServerGoodsmentCtrl($http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer){
		$scope.title="商品列表";
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
			type:'POST',
			url:'../mall/goods/findByPage',
			dataSrc:'data'
		})
		.withOption('processing',true)
		.withOption('paging',true)
		.withOption('serverSide',true)
		.withOption('createdRow',function(row,data,dataIndex){
			$compile(angular.element(row).contents())($scope);
		})
		.withOption('headerCallback', function(header) {
            if (!$scope.headerCompiled) {
                $scope.headerCompiled = true;
                $compile(angular.element(header).contents())($scope);
            }
        })
        .withOption('stateSave', true)
        .withOption('rowCallback',rowCallback)
		.withPaginationType('full_numbers')
		.withLanguageSource('./app/language/chinese.json')

		$scope.$on('handleRequest',function(){

		});
		vm.dtColumns=[
		              DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
		            	  vm.selected[full.id]=false;
		            	  return "<input type='checkbox' ng-model=\"showCase.selected['"+data.id+"']\" ng-click=\"showCase.toggleOne(showCase.selected)\">";
		              }),
		              DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
		              DTColumnBuilder.newColumn(null).withTitle('图片').notSortable().renderWith(imgHtml),
			          DTColumnBuilder.newColumn('goodsname').withTitle('商品标题').notSortable(),
			          DTColumnBuilder.newColumn('nname').withTitle('分类').notSortable(),
			          DTColumnBuilder.newColumn('goodstypename').withTitle('商品类型').notSortable(),
			          DTColumnBuilder.newColumn('price').withTitle('价格').notSortable(),
			          DTColumnBuilder.newColumn('sort').withTitle('排序').notSortable(),
			          DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			          DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			          DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			          DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function imgHtml(data,type,full,meta){
			return '<img style="width:50px;height:50px;" ng-src="'+data.smainimg+'" alt="Photo" />';
		}
		function actionHtml(data,type,full,meta){
			return "<button  ng-click=\"edit('"+data.id+"')\" class='btn btn-warning'><i class='fa fa-edit'></i></button>"+
				"<button ng-click=\"commentment('"+data.id+"','"+data.goodsname+"')\" class='btn btn-success'><i class='fa fa-comments-o'></i></button>";
		}

		function reloadData(){
			var resetPaging = false;
	        vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}


		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.goodsname!=undefined){
				searchParams.name=$scope.goodsname;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}

		//表格中编辑按钮
		$scope.edit=function(id,name){
			$location.path('/goods').search({'operate':'edit','id':id});
		}

		/**
		 * 跳转进入商品评论列表
		 * @param id
         * @param name
         */
		$scope.commentment=function(id,name){
			$location.path('/goodscommentonement').search({'operate':'find','id':id,'goodsname':name});
		}
		/**
		 * 跳转到添加商品类型和参数页面
		 */
		$scope.save=function(){
			$location.path('/goods').search({'operate':'save'});
		}

		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
						show:true,
						msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
						show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/goods/del',
					data:{
						'goodsid':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.errors={};
						$scope.info={
								show:true,
								msg:'删除成功'
						}
						reloadData();
					}

				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;

		}

		function someClickHandler(info) {
	        vm.message = info.id + ' - ' + info.name;

	    }
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
	        $('td', nRow).bind('click', function() {
	            $scope.$apply(function() {
	                vm.someClickHandler(aData);

	            });
	        });
	        return nRow;
		}

	}
});
